home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue99 / CPROG6.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-05  |  2.6 KB  |  93 lines

  1. /* CPROG6.CPP - some variations on a WHILE loop */
  2.  
  3. #include <stdio.h>
  4.  
  5. main()
  6. {
  7. int i;
  8.  
  9.  
  10.     printf("CASE 1\n");
  11.  
  12.     i=0;                  // Set i to zero
  13.     printf("i is now...");
  14.     while( i < 6 )                // If i hasn't reached six yet...
  15.         {
  16.         printf(" %d", i);    // Print value of i
  17.         i++;            // Increment i
  18.         }
  19.     putchar('\n');              // Putchar prints a single character
  20.                     // \n is the newline character
  21.                     // which needs to be in quote
  22.                     // marks to mark it as a single
  23.                     // character. Putchar expects an
  24.                     // integer... putchar(33) prints
  25.                     // ASCII code 33 (a ! character)
  26.                     // '\n', being a recognised
  27.                     // code for 'do a carriage return
  28.                     // and line feed', is converted
  29.                     // to the value 10. Under UNIX
  30.                     // 10 is both a CR and a LF. On
  31.                     // the PC CR is 13, and LF is 10.
  32.                     // '\n' is therefore converted
  33.                     // into two characters, but this is
  34.                     // a translation into a hardware-
  35.                     // specific implementation detail.
  36.                     // Conceptually, '\n' is a single
  37.                     // character as far as C++ is
  38.                     // concerned.
  39.     ////////////////////////////////////////////////////////
  40.     printf("CASE 2\n");
  41.     i=0;
  42.     printf("i is now...");
  43.  
  44.     while( i < 6 )
  45.        printf(" %d", i++);        // C++ takes an 'evaluate it then
  46.                     // use the resulting number' approach
  47.                     // so you can have i incremented
  48.                     // in this context. You could also
  49.                     // do something like i+=2 which would
  50.                     // yield 0+2=2 as the first thing
  51.                     // that %d prints. However, i++ is
  52.                     // slightly special. It means
  53.                     // 'use the value, then increment it'
  54.                     // so, first time round the loop, %d
  55.                     // is given the value 0 and then i
  56.                     // is increased to 1.
  57.  
  58.     putchar('\n');
  59.  
  60.     ////////////////////////////////////////////////////////
  61.  
  62.     printf("CASE 3\n");
  63.     i=0;
  64.     printf("i is now...");
  65.  
  66.     while( i++ < 6 )        // i++ works here but, because of
  67.         printf(" %d", i);    // the different position, the
  68.                     // first and last values fed to %d
  69.                     // are different. i is tested then
  70.                     // incremented, so the first time
  71.                     // printf() is executed i is
  72.                     // already 1. %d will see a value of
  73.                     // 6 becuase when i is 5 it is
  74.                     // incremented immediately after
  75.                     // the test.
  76.     putchar('\n');
  77.  
  78.     ////////////////////////////////////////////////////////
  79.  
  80.     printf("CASE 4\n");
  81.     i= -1;
  82.     printf("i is now...");
  83.  
  84.     while( i < 6 )            // ++i says 'increment the value then
  85.         printf(" %d", ++i);    // use it' so, the first one %d
  86.                     // sees is -1 + 1 = 0. When i is 5,
  87.                     // it goes up to 6 in the printf()
  88.                     // line before the loop is aborted
  89.                     // by the WHILE test.
  90.  
  91.     putchar('\n');
  92. }
  93.